package com.whc.friend.controller;


import com.whc.common.domain.Result;
import com.whc.friend.domain.vo.FriendVo;
import com.whc.friend.service.FriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "朋友接口")
@RestController
@RequestMapping("/client/friend")
@Slf4j
public class FriendController {
    @Autowired
    private FriendService friendService;

    @ApiOperation("添加朋友关系")
    @PutMapping("/addFriend")
    public Result addFriend(@RequestParam("pid") Long pid, @RequestParam("fid") Long fid){
        //判断是否已经是朋友关系，是就直接返回
        List<FriendVo> friends = friendService.getAllFriends(pid);
        for(FriendVo f:friends){
            if(f.getId()==fid){
                return Result.success();
            }
        }
        friendService.addFriend(pid,fid);
        return Result.success();
    }

    @ApiOperation("查询所有朋友")
    @GetMapping("/getAllFriends")
    public Result<List<FriendVo>> getAllFriends(@RequestParam("pid") Long pid){
        List<FriendVo> friends=friendService.getAllFriends(pid);
        return Result.success(friends);
    }

}
